package com.sample;

import dk.pekilidi.dod.Human;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.runtime.rule.FactHandle;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.builder.*;
import org.drools.compiler.PackageBuilder;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.rule.Package;
import org.drools.runtime.StatefulKnowledgeSession;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * This is a sample file to launch a rule package from a rule source file.
 */
public class DroolsTest {

    public static final void main(String[] args) {
        try {
        	
        	//load up the rulebase
            KnowledgeBase kbase = readKnowledgeBase();

            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            //KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);

            //go !
            List<Human> result = new ArrayList<Human>();
            List<FactHandle> handleList = new ArrayList<FactHandle>();
            long timeBefore = new Date().getTime();
            for(int i=0; i<1; i++){
                Human human = new Human();
                handleList.add(ksession.insert(human));

            }
            ksession.fireAllRules();
            for (FactHandle factHandle : handleList) {
                Human human = (Human)ksession.getObject(factHandle);
                System.out.println("Human: " + human);
            }
            long timeAfter = new Date().getTime();
//
            System.out.println("Time to execute rules: " + (timeAfter - timeBefore) + " msecs");
            // stop logging
            //logger.close();

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {

/*        final SpreadsheetCompiler converter = new SpreadsheetCompiler();
        final String drl = converter.compile( ResourceFactory.newClassPathResource("HandUseTbl.xls").getInputStream(), InputType.XLS );
        System.out.println(drl);*/



        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("BeingConfig.drl"), ResourceType.DRL);
        kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
        kbuilder.add(ResourceFactory.newClassPathResource("AttributeGroupTbl.xls"), ResourceType.DTABLE);
        kbuilder.add(ResourceFactory.newClassPathResource("DamageBonusTbl.xls"), ResourceType.DTABLE);
        kbuilder.add(ResourceFactory.newClassPathResource("HandUseTbl.xls"), ResourceType.DTABLE);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

	public static class Message {
		public static final int HELLO = 0;
		public static final int GOODBYE = 1;

		private String message;

		private int status;

		public String getMessage() {
			return this.message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public int getStatus() {
			return this.status;
		}

		public void setStatus( int status ) {
			this.status = status;
		}
	}

}
